#!/usr/bin/env python3
from functools import partial
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer, SimpleHTTPRequestHandler
import os, subprocess, sys

def serve_page(
  HandlerClass=BaseHTTPRequestHandler,
  ServerClass=ThreadingHTTPServer,
  protocol='HTTP/1.0',
  port=8000,
  bind=''):

  server_addr = (bind, port)

  HandlerClass.protocol_version = protocol
  with ServerClass(server_addr, HandlerClass) as httpd:
    sa = httpd.socket.getsockname()
    print(f"serving http on http://{sa[0]}:{sa[1]}")
    try:
      httpd.serve_forever()
    except KeyboardInterrupt:
      print('exit')
      sys.exit(0)

def start_serve_page():
  serve_dir = os.path.join(os.getcwd(), 'web')
  handler_class = partial(SimpleHTTPRequestHandler, directory=serve_dir)
  serve_page(HandlerClass=handler_class, port=8000, bind='0.0.0.0')